window.onload = function (){
    showLoginInfo();
    findByItem(1);
}

function showLoginInfo(){
    axios.get("/user/getLoginUser",null).then(e => {
        var info = e.data;
        if (info != null && info !== '' && info.level=='顾客'){
            $('infoDiv').innerHTML = "欢迎光临: "+info.name;
        }
        else if (info != null && info !== '' && info.level=='管理员'){
            location.href = "/manager/orderManager.html"
        }
        else if (info != null && info !== '' && info.level=='送货员'){
            location.href = "/send/send.html"
        }
        else {
            $('infoDiv').innerHTML = "你好，请先<a href='/login.html'>登录</a>";
        }
    })
}
function findByItem(pageNO){
    axios.get("/product/findByItem",{
        params:{
            pageNO,
            name:$('productName').value,
            startDate:$('startDate').value,
            endDate:$('enDate').value
        }
    }).then(e => {
        var info = e.data;
        var str = "";
        for (var i=0;i<info.records.length;i++){
            var obj = info.records[i];
            str += `<div><img src="/productImg/${obj.imgPath}" width="200px" height="200px"><br>
                       ${obj.name}<br>单价:${obj.price}<br>已售:${obj.sailCount}件<br>
                       <input type="button" value="查看" onclick="javascript:location.href='/product/product.html?id=${obj.id}'">
                       <input type="button" value="购买"
                       onclick=javascript:location.href='/customer/shopCart.html?productId=${obj.id}'></div>`
        }
        $('productData').innerHTML = str;
        var pageStr = "";
        for (var i=1;i<=info.pages;i++){
            pageStr += `<a href="javascript:findByItem(${i})" style="margin-right: 20px">${i}</a>`
        }
        $('cutDiv').innerHTML = pageStr;
    })
}

function findByManager(){
    window.location.href = "/manager/orderManager.html";
}
function findBySend(){
    window.location.href = "/send/send.html";
}